//
//  WLLComposeBottomView.swift
//  WLLWeibo
//
//  Created by 王乐乐 on 16/8/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
//底部视图按钮的枚举
enum WLLComposeBottomViewType : Int {
     /// 图片
    case Picture = 0
    /// @
    case Mention = 1
    /// #
    case Trend = 2
    /// 笑脸
    case Emoticon = 3
    /// +
    case Add = 4
}

class WLLComposeBottomView: UIView {
    
    var emoticonButton : UIButton?
    
    var isEmoticon : Bool = false{
        didSet{
            /*
             true  代表当前为 代表显示为表情键盘  -> 显示为 键盘图片
             false 代表当前为 代表显示为系统键盘  -> 显示为 笑脸图片
             */
            // true  代表当前 显示为表情键盘  -> 显示为 键盘图片//
            let imgName :String
            if isEmoticon {
                imgName = "compose_keyboardbutton_background"
            }else{
                // false 代表当前 显示为系统键盘  -> 显示为 笑脸图片

                imgName = "compose_emoticonbutton_background"
            }
             // 设置图片
            emoticonButton?.setImage(UIImage(named: imgName) , forState: UIControlState.Normal)
            emoticonButton?.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
            
        }
    }
    
    
    
    //声明闭包
    var closure :((type: WLLComposeBottomViewType) -> ())?
    

   override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
     //MARK: - 设置视图
    private func setupUI() {
//        backgroundColor = UIColor(patternImage: UIImage(named:"compose_toolbar_background")!)
        
        //添加按钮
        let pictureButton = addChildButtons("compose_toolbar_picture", type: .Picture)
        let mentionButton = addChildButtons("compose_mentionbutton_background", type: .Mention)
        let trendButton = addChildButtons("compose_trendbutton_background", type: .Trend)
         emoticonButton = addChildButtons("compose_emoticonbutton_background", type: .Emoticon)
        let addButton = addChildButtons("compose_add_background", type: .Add)
       
        //添加约束
        //01
        pictureButton.snp_makeConstraints { (make) in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        //02
        mentionButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        //03
        trendButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton!)
        }
        //04
        emoticonButton!.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        //05
        addButton.snp_makeConstraints { (make) in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
        }
        
    }
    
    func addChildButtons(imgName:String,type : WLLComposeBottomViewType ) -> UIButton {
        let button = UIButton()
       // 设置tag //type.rawValue 通过枚举名 得到对应的枚举值
        button.tag = type.rawValue
        //添加事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            
        button.setImage(UIImage(named:imgName) , forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        
        addSubview(button)
         return button
    }
    //按钮的点击事件
    func buttonClick(button:UIButton) {
        print(button.tag)
        
        closure?(type: WLLComposeBottomViewType(rawValue:button.tag)!)
        
    }
    
    
    
}
